package net.zdreamer.geracomium.ui.more;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.geracomium.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import net.zdreamer.geracomium.adapter.MessageNotifiAdapter;
import net.zdreamer.geracomium.entity.MessageNotiInfos;

public class MessageNotificationActivity extends Activity implements
        OnClickListener {
    private ImageView back, share;
    private PullToRefreshListView mPullRefreshListView;
    private MessageNotifiAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_notification);
        init();
    }

    private void init() {
        back = (ImageView) findViewById(R.id.back);
        back.setImageResource(R.drawable.selector_title_back);
        back.setOnClickListener(this);
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.message_list);
        mPullRefreshListView.setMode(Mode.BOTH);
        MessageNotiInfos msgnoti = new MessageNotiInfos();
        mAdapter = new MessageNotifiAdapter(this, msgnoti.messagenotiInfos,
                R.layout.list_item);
        mPullRefreshListView.setAdapter(mAdapter);
        mPullRefreshListView.setOnRefreshListener(orl);


    }

    private OnRefreshListener<ListView> orl = new OnRefreshListener<ListView>() {

        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            String label = DateUtils.formatDateTime(getApplicationContext(),
                    System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);

            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(
                    "上次刷新：" + label);

            Thread a = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message message = new Message();
                    message.what = 0;
                    h.sendMessage(message);
                }
            });
            a.start();
        }
    };
    Handler h = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                MessageNotiInfos msgnoti = new MessageNotiInfos();
                mAdapter = new MessageNotifiAdapter(
                        MessageNotificationActivity.this,
                        msgnoti.messagenotiInfos, R.layout.list_item);
                mPullRefreshListView.setAdapter(mAdapter);
                mPullRefreshListView.onRefreshComplete();
            }
        }

    };

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back:
                Intent intent = new Intent(MessageNotificationActivity.this,
                        PersonalCenterActivity.class);
                startActivity(intent);
                MessageNotificationActivity.this.finish();

                break;
            default:
                break;
        }
    }

}
